/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package chameleon.view.command;

import chameleon.model.NuGatManager.NuGatManager;
import chameleon.model.transitionFormat.AbstractTransition;
import chameleon.model.transitionFormat.AutomataTransition;
import chameleon.model.transitionFormat.LtlTransition;
import chameleon.model.transitionFormat.SmvTransition;
import chameleon.view.Tag;
import chameleon.view.input.Input;
import java.io.File;

/**
 * Only to NuGat.
 * @author simon
 */
class AddTransitionCmd extends Command {

    private int player_;
    private int format_;
    private AbstractTransition transition_;
    private boolean correct_ = true;

    public AddTransitionCmd(Input input, String player, String format, String transition
            , boolean help) {
        super(input,false,help);
        if (!help){
            player_ = getPlayer(player);
            format_ = getFormat(format);
            setTransition(transition);
        }
    }

    @Override
    public Input execute() {
        if (correct_){
            if (!help_){
                InternalCheckCommand check = new InternalCheckCommand(input_, 2);
                if (check.isOk()){
                    NuGatManager manager = (NuGatManager) input_.getManager();
                    if(manager == null)
                        outputManager_.displayString("Please use build_model before!\n");
                    else
                        input_.setManager(manager.addTransition(player_, transition_));
                    outputManager_.displayString("\n");
                }
            }
            else{
                outputManager_.displayString("addTransition add new transition(s) to the game\n"
                        + "the syntax is addTransition -Player(P1|P2) -format(-ltl|-aut|-smv) -(String|file) \n"
                        + "-P1|P2 on wich player we add the transition\n"
                        + "-ltl the input is an LTL formula (on string, given on a file)\n"
                        + "-aut the input is an automaton (on GOAL format, give the file link), You must labelled"
                        + "node by the correspondance state in the smv game structure\n"
                        + "-smv the input is a smv transition (on string format)\n"
                        + "The format for smv transition is source -> next(target)\n"
                        + "where source and target are boolean formula\n");
            }
        }
        return input_;

    }

    private int getPlayer(String player){
        if (player.equals("P1"))
            return 1;
        else
            return 2;
    }

    private int getFormat(String format) {
        if (format.equals("ltl"))
            return Tag.LTL;
        else if (format.equals("smv"))
            return Tag.SMV;
        else 
            return Tag.AUTOMATA;

    }

    private void setTransition(String transition) {
        File inputFile = new File (transition);
        if (inputFile.isFile()){
            String extension = transition.substring(transition.indexOf("." ));
            if (extension.matches(".gff") && format_ == Tag.AUTOMATA){
                transition_ =  new AutomataTransition(inputFile);
            }
            else if (format_==Tag.LTL){
                transition_ = new LtlTransition(inputFile);
            }
        }
        else if (format_==Tag.SMV)
            transition_ =new SmvTransition(transition);
        else{
            correct_ = false;
            outputManager_.displayString("Link given on input to transition is not valid\n");
        }

    }

    private File getMapTable(String correspondanceTable) {
        if (correspondanceTable != null){
            File inputFile = new File (correspondanceTable);
            if (inputFile.isFile()){
                return inputFile;
            }
            else{
                correct_ = false;
                outputManager_.displayString("Link given on input for map table is not valid!\n");
                return null;
            }
        }
        else
            return null;
    }

}
